package com.exocr;

import android.text.TextUtils;

import com.baidu.ocr.ui.camera.CameraActivity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class OCRTypes implements Serializable {
    private static final long serialVersionUID = -3649159633494103143L;

    public static class OCRCommonBranch{
        //通用文字
        public static final String type_general_basic="general_basic";
        public static final String type_general="general";//（含位置信息版）
        //高精度 可识别生僻字
        public static final String type_accurate_basic="accurate_basic";//（高精度版）
        public static final String type_accurate="accurate";//（高精度含位置版）
    }
    public static class OCRCardBranch{
        //卡证文字
        public static final String type_idcard="idcard";//身份证识别
        public static final String type_bankcard="bankcard";//银行卡识别
        public static final String type_business_license="business_license";//营业执照识别
        public static final String type_business_card="business_card";//名片识别
        public static final String type_passport="passport";//护照识别
        public static final String type_HK_Macau_exitentrypermit="HK_Macau_exitentrypermit";//港澳通行证识别
        public static final String type_taiwan_exitentrypermit="taiwan_exitentrypermit";//台湾通行证识别
        public static final String type_household_register="household_register";//户口本识别
        public static final String type_birth_certificate="birth_certificate";//出生医学证明识别
        public static final String type_multi_card_classify="multi_card_classify";//多卡证类别检测
    }
    public static class OCRBillBranch{
        //票据文字
        public static final String type_vat_invoice="vat_invoice";//增值税发票识别
        public static final String type_quota_invoice="quota_invoice";//定额发票识别
        public static final String type_invoice="invoice";//通用机打发票识别
        public static final String type_train_ticket="train_ticket";//火车票识别
        public static final String type_taxi_receipt="taxi_receipt";//出租车票识别
        public static final String type_air_ticket="air_ticket";//行程单识别
        public static final String type_bus_ticket="bus_ticket";//汽车票识别
        public static final String type_toll_invoice="toll_invoice";//通行费发票识别
        public static final String type_receipt="receipt";//通用票据识别
        public static final String type_insurance_documents="insurance_documents";//保单识别
        public static final String type_lottery="lottery";//彩票识别
    }
    public static class OCRCarBranch{
        //汽车场景
        public static final String type_vehicle_license="vehicle_license";//行驶证识别
        public static final String type_driving_license="driving_license";//驾驶证识别
        public static final String type_license_plate="license_plate";//车牌识别
        public static final String type_vin_code="vin_code";//VIN码识别
        public static final String type_vehicle_invoice="vehicle_invoice";//机动车销售发票识别
        public static final String type_vehicle_certificate="vehicle_certificate";//车辆合格证识别
    }
    public static class OCREducateBranch{
        //教育场景文字
        public static final String type_handwriting="handwriting";//手写文字识别
        public static final String type_formula="formula";//公式识别
    }
    public static class OCRExtraBranch{
        //其他文字识别
        public static final String type_webimage="webimage";//网络图片文字识别
        public static final String type_webimage_loc="webimage_loc";//网络图片文字识别（含位置版）
        public static final String type_doc_analysis="doc_analysis";//文档版面分析与识别
        public static final String type_meter="meter";//仪器仪表盘读数识别
        public static final String type_request="form_ocr/request";//表格文字识别(异步接口)
        public static final String type_form="form";//表格文字识别(同步接口)
        public static final String type_numbers="numbers";//数字识别
        public static final String type_qrcode="qrcode";//二维码识别
        public static final String type_seal="seal";//印章检测
        public static final String type_facade="facade";//门脸文字识别
    }
    public static class OCRXFBranch{
        //http[s]: //webapi.xfyun.cn/v1/service/v1/ocr/handwriting
        public static final String type_handwriting="handwriting";//手写文字识别
        //http[s]: //webapi.xfyun.cn/v1/service/v1/ocr/general
        public static final String type_general="general";//印刷文字识别
        //http[s]: //webapi.xfyun.cn/v1/service/v1/ocr/recognize_document
        public static final String type_recognize_document="recognize_document";//印刷文字识别（多语种）
        //http[s]: //webapi.xfyun.cn/v1/service/v1/ocr/business_card
        public static final String type_business_card="business_card";//名片识别
        //http[s]: //webapi.xfyun.cn/v1/service/v1/ocr/idcard
        public static final String type_idcard="idcard";//身份证识别
        //http[s]: //webapi.xfyun.cn/v1/service/v1/ocr/bankcard
        public static final String type_bankcard="bankcard";//银行卡识别
        //http[s]: //webapi.xfyun.cn/v1/service/v1/ocr/business_license
        public static final String type_business_license="business_license";//营业执照识别
        //http[s]: //webapi.xfyun.cn/v1/service/v1/ocr/invoice
        public static final String type_invoice="invoice";//增值税发票识别
        //http[s]: //rest-api.xfyun.cn/v2/itr
        //拍照速算识别与公式识别 方法一致仅参数不同，便于判断给公式识别增加了后缀_et
        public static final String type_itr="itr";//拍照速算识别
        public static final String type_itr_et="itr_et";//公式识别
        //http[s]: //tyocr.xfyun.cn/v2/ocr
        public static final String type_ocr="ocr";//指尖文字识别
    }
    public static final String ocr_type_common="OCRCommon";//通用文字
    public static final String ocr_type_card="OCRCard";//卡证文字
    public static final String ocr_type_bill="OCRBill";//票据文字
    public static final String ocr_type_car="OCRCar";//汽车场景
    public static final String ocr_type_educate="OCREducate";//教育场景文字
    public static final String ocr_type_extra="OCRExtra";//其他文字识别
    public static final String ocr_type_xunfei="OCRXunFei";//其他文字识别

    public static String urlRoot_xf_v1_ocr="https://webapi.xfyun.cn/v1/service/v1/ocr/";
    public static String urlRoot_xf_v2_itr="https://rest-api.xfyun.cn/v2/";
    public static String urlRoot_xf_v2_ocr="https://tyocr.xfyun.cn/v2/";
    public static String urlRoot_bd_v1="https://aip.baidubce.com/rest/2.0/ocr/v1/";
    private String ocrType=null;
    private String ocrBranch=null;
    private String ocrFunc=null;
    private String urlRoot=null;

    private String appid;//讯飞必须
    private String apiKey;//讯飞必须
    private String apiKey_V2;//ocrFunc:OCRXunFei_itr or OCRXunFei_ocr必须
    private String apiSecretKey;//讯飞必须
    private int frontOrBackType=0;//0正面 1反面
    private String cameraMaskType;
    public OCRTypes(String ocrType,String ocrBranch){
        this(ocrType,ocrBranch,0);
    }
    public OCRTypes(String ocrType,String ocrBranch,int frontOrBackType){
        setFrontOrBackType(frontOrBackType);
        setOcrBranch(ocrBranch);
        setOcrType(ocrType);
    }

    public String getOcrType() {
        return ocrType;
    }

    public void setOcrType(String ocrType) {
        this.ocrType = ocrType;
    }

    public String getOcrBranch() {
        return ocrBranch;
    }

    public void setOcrBranch(String ocrBranch) {
        this.ocrBranch = ocrBranch;
        if (ocrBranch!=null){
            if (ocrBranch.equals(OCRXFBranch.type_itr_et)){
                ocrFunc=OCRXFBranch.type_itr;
            }else{
                ocrFunc=ocrBranch;
            }
        }
    }

    public String getOcrFunc() {
        return ocrFunc;
    }

    public void setOcrFunc(String ocrFunc) {
        this.ocrFunc = ocrFunc;
    }

    public String getUrlRoot() {
        if (urlRoot==null){
            if (ocrType!=null){
                if (ocrType.equals(ocr_type_xunfei)){
                    if (OCRXFBranch.type_itr.equals(ocrFunc)){
                        urlRoot=urlRoot_xf_v2_itr;
                    }else if (OCRXFBranch.type_ocr.equals(ocrFunc)) {
                        urlRoot=urlRoot_xf_v2_ocr;
                    }else {
                        urlRoot=urlRoot_xf_v1_ocr;
                    }
                }else{
                    urlRoot=urlRoot_bd_v1;
                }
            }
        }
        return urlRoot;
    }

    public void setUrlRoot(String urlRoot) {
        this.urlRoot = urlRoot;
    }

    public int getFrontOrBackType() {
        return frontOrBackType;
    }

    public void setFrontOrBackType(int frontOrBackType) {
        this.frontOrBackType = frontOrBackType;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getApiKey() {
        if (OCRXFBranch.type_itr.equals(ocrFunc)||
                OCRXFBranch.type_ocr.equals(ocrFunc)){
            return apiKey_V2;
        }
        return apiKey;
    }

    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

    public String getApiKey_V2() {
        return apiKey_V2;
    }

    public void setApiKey_V2(String apiKey_V2) {
        this.apiKey_V2 = apiKey_V2;
    }

    public String getApiSecretKey() {
        return apiSecretKey;
    }

    public void setApiSecretKey(String apiSecretKey) {
        this.apiSecretKey = apiSecretKey;
    }

    private Map<String,Object> ocrOptions=null;
//    private int sizeLimit=0;

    public Map<String, Object> getOcrOptions() {
        if (ocrOptions==null){
            ocrOptions=new HashMap<>();
            if (ocrType!=null&&!ocrType.equals(ocr_type_xunfei)) {
                if (ocrFunc!=null&&ocrFunc.equals(OCRCardBranch.type_idcard)){
                    ocrOptions.put("id_card_side",frontOrBackType==0?"front":"back");
                }
            }
        }
        return ocrOptions;
    }

    public void setOcrOptions(Map<String, Object> ocrOptions) {
        this.ocrOptions = ocrOptions;
    }

//    public int getSizeLimit() {
//        if (sizeLimit<=0&&ocrFunc!=null) {
//            if (ocrFunc.equals(OCRCommonBranch.type_general_basic)||
//                    ocrFunc.equals(OCRCommonBranch.type_general)||
//                    ocrFunc.equals(OCRCommonBranch.type_accurate)||
//                    ocrFunc.equals(OCRExtraBranch.type_form)) {
//                sizeLimit=1024000;
//            }else {
//                sizeLimit=4096000;
//            }
//        }
//        return sizeLimit;
//    }
//
//    public void setSizeLimit(int sizeLimit) {
//        this.sizeLimit = sizeLimit;
//    }

    public boolean isKeyAvailable() {
        if (ocrType!=null) {
            if (ocrType.equals(OCRTypes.ocr_type_xunfei)){
                if (!TextUtils.isEmpty(appid)&&
                        !TextUtils.isEmpty(apiKey) &&
                        !TextUtils.isEmpty(apiSecretKey)){
                    return true;
                }
            }else {
                if (!TextUtils.isEmpty(apiKey) &&
                        !TextUtils.isEmpty(apiSecretKey)){
                    return true;
                }
            }
        }
        return false;
    }

    public String getCameraMaskType() {
        if (cameraMaskType==null){
            if (ocrFunc!=null&&ocrType!=null) {
                if (ocrType.equals(ocr_type_xunfei)) {
                    if (ocrFunc.equals(OCRXFBranch.type_business_card)) {

                    } else if (ocrFunc.equals(OCRXFBranch.type_idcard)) {
                        return CameraActivity.CONTENT_TYPE_BANK_CARD;
                    } else if (ocrFunc.equals(OCRXFBranch.type_bankcard)) {
                        return CameraActivity.CONTENT_TYPE_BANK_CARD;
                    }
                } else {
                    if (ocrFunc.equals(OCRCardBranch.type_idcard)) {
                        return frontOrBackType == 0 ? CameraActivity.CONTENT_TYPE_ID_CARD_FRONT : CameraActivity.CONTENT_TYPE_ID_CARD_BACK;
                    } else if (ocrFunc.equals(OCRCardBranch.type_bankcard)) {
                        return CameraActivity.CONTENT_TYPE_BANK_CARD;
                    } else if (ocrFunc.equals(OCRCardBranch.type_passport)) {
                        return CameraActivity.CONTENT_TYPE_PASSPORT;
                    } else if (ocrFunc.equals(OCRCardBranch.type_business_card)) {
                    }
                }
            }
            if (cameraMaskType==null){
                cameraMaskType=CameraActivity.CONTENT_TYPE_GENERAL;
            }
        }
        return cameraMaskType;
    }

    public void setCameraMaskType(String cameraMaskType) {
        this.cameraMaskType = cameraMaskType;
    }
}
